﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using FileMonitor.Configuration.Prototype;
namespace MonitorProvider.Prototype.FileReader
{
    public class DefaultTXTFileReader:FileReader.FileReaderBase
    {
        private string _splitChar;
        private string _splitArr;
        private bool isInitialize = false;

        public DefaultTXTFileReader() : base() { }

        public override void Initialize(FileType fileType, FileProcessConfiguration fileProcessConfiguration)
        {
            if (!string.IsNullOrEmpty(fileProcessConfiguration.SplitChar))
            {
                this._splitChar = fileProcessConfiguration.SplitChar;
            }
            else
            {
                this._splitChar = "^";
            }
            this._splitArr = fileProcessConfiguration.SplitArr;
            this.isInitialize = true;
            base.Initialize(fileType, fileProcessConfiguration);
        }

        public override object GetFileProcessedResult(string fileName, DataTable dtScheml, Guid batchId)
        {
            DataTable dtContent;
            if (string.IsNullOrEmpty(this._splitArr))
            {
                dtContent = FileUtility.FileUtility.ReadFileContentToDataTable(fileName, dtScheml, batchId, this._splitChar);
            }
            else
            {
                string[] inputArr = this._splitArr.Split(",".ToCharArray());
                int[] splitLenthArr = Array.ConvertAll<string, int>(inputArr, s => { return Int32.Parse(s); });
                dtContent = FileUtility.FileUtility.ReadFileContentToDataTable(fileName, dtScheml, batchId, splitLenthArr);
            }
            return dtContent;
        }
    }
}
